/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.click.app.dao.hibernate;

import com.click.app.dao.DistinctiveDao;
import com.click.app.model.Distinctive;
import com.click.app.model.Material;
import java.util.ArrayList;
import java.util.List;
import org.springframework.orm.hibernate3.HibernateTemplate;

/**
 *
 * @author orgale
 */
public class DistinctiveDaoHibernate extends GenericDaoHibernate<Distinctive, Long> implements DistinctiveDao {

    public DistinctiveDaoHibernate() {
        super(Distinctive.class);
    }

    public List<Distinctive> findById(Long id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Distinctive> findByMaterial(Material material) {
        List<Distinctive> distinctives = new ArrayList<Distinctive>();
        distinctives.addAll(material.getDistinctives());
        return distinctives;
    }

    public List<Distinctive> findDistinctives() {
        List<Distinctive> dList = new ArrayList<Distinctive>();
        List<Distinctive> all = (List<Distinctive>) getHibernateTemplate().find("from Distinctive as d order by d.name");
        dList.add(all.get(0));
	String name = all.get(0).getName();
        for (int i = 1; i < all.size(); i++) {
		if(!all.get(i).getName().equals(name)){
			dList.add(all.get(i));
			name = all.get(i).getName();
		}
        }
        return dList;
    }

    public List<String> distinctivesNames() {
        return getHibernateTemplate().find("select distinct name from Distinctive");
    }

    public List<Distinctive> findDistinctivesName(String name) {
        return getHibernateTemplate().find("from Distinctive where name=?", name);
    }

    public List<Distinctive> findDistinctivesByValue(String value) {
        return getHibernateTemplate().find("from Distinctive where value=?", value);
    }

    public List<Distinctive> findDistinctivesByNameAndValue(String name, String value){
        String[] args = {name, value};
        return getHibernateTemplate().find("from Distinctive where name=? and value=?", args);
    }

}
